package com.lzx.iteam;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.ToggleButton;
import com.lzx.iteam.bean.AttendanceData;
import com.lzx.iteam.net.AsynHttpClient;
import com.lzx.iteam.net.GetMsgHttpReceiver;
import com.lzx.iteam.net.LocalLogin;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Megan on 16/4/12.
 */
public class AttendanceManageActivity extends Activity implements View.OnClickListener {


    private LinearLayout mLlBack;
    private RelativeLayout mRlDeviceChange,mRlSetting;
    private RelativeLayout mRlNotice,mRlSub;
    private ToggleButton mTbNotice;
    private String mNotice;
    private AttendanceData attendanceData;
    private final int SET_NOTIFICATION = 10001;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SET_NOTIFICATION:
                    if (msg.arg1 == 0) {
                        attendanceData.setIsAlert(mNotice);
                    } else {
                        Toast.makeText(AttendanceManageActivity.this, (String) msg.obj,
                                Toast.LENGTH_LONG).show();
                    }
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.attendance_manage_layout);
        initViews();
        getIntentData();
    }

    private void initViews() {
        mLlBack = (LinearLayout) findViewById(R.id.ll_attendance_manage_back);
        mRlDeviceChange = (RelativeLayout) findViewById(R.id.rl_attendance_manage_device_change);
        mRlNotice = (RelativeLayout) findViewById(R.id.rl_attendance_manage_notice);
        mTbNotice = (ToggleButton) findViewById(R.id.tb_attendance_manage_notice);
        mRlSetting = (RelativeLayout) findViewById(R.id.rl_attendance_manage_setting);
        mRlSub = (RelativeLayout) findViewById(R.id.rl_attendance_my_sub);

        mLlBack.setOnClickListener(this);
        mRlDeviceChange.setOnClickListener(this);
        mRlNotice.setOnClickListener(this);
        mTbNotice.setChecked(false);
        mRlSetting.setOnClickListener(this);
        mRlSub.setOnClickListener(this);
    }

    private void getIntentData() {
        Intent intent = getIntent();
        attendanceData = (AttendanceData) intent.getSerializableExtra("attendanceData");
        mNotice = attendanceData.getIsAlert();
        setNotice(mNotice);

        String isAdmin = attendanceData.getIsAdmin();
        if ("1".equals(isAdmin)){
            mRlSetting.setVisibility(View.VISIBLE);
        }else{
            mRlSetting.setVisibility(View.GONE);
        }
        int formType = attendanceData.getFormType();
        if (formType == 1){
            mRlSub.setVisibility(View.VISIBLE);
        }else{
            mRlSub.setVisibility(View.GONE);
        }

    }


    private void setNotice(String notice) {
        if ("0".equals(notice)) {
            mTbNotice.setChecked(false);
        } else if ("1".equals(notice)) {
            mTbNotice.setChecked(true);
        }
    }

    private int getNotice() {
        if (mTbNotice.isChecked()) {
            mNotice = "1";
            return 1;
        } else {
            mNotice = "0";
            return 0;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_attendance_manage_back:
                Intent back = new Intent();
                back.putExtra("attendance", attendanceData);
                setResult(RESULT_OK, back);
                finish();
                break;
            case R.id.rl_attendance_manage_device_change:
                Intent intent = new Intent(AttendanceManageActivity.this, AttendanceDevicesActivity.class);
                intent.putExtra("groupId", attendanceData.getGroupId());
                startActivity(intent);
                break;
            case R.id.rl_attendance_manage_notice:
                mTbNotice.toggle();
                setAttendanceNotificationMsg();
                break;
            case R.id.rl_attendance_manage_setting:
                Intent attendance = new Intent(AttendanceManageActivity.this, AttendanceCreateActivity.class);
                attendance.putExtra("group_id", attendanceData.getGroupId());
                attendance.putExtra("group_name", attendanceData.getGroupName());
                attendance.putExtra("user_type", "2");
                attendance.putExtra("contact_id", "0");
                startActivity(attendance);
                break;
            case R.id.rl_attendance_my_sub:
                Intent sub = new Intent(this,AttendanceMySubActivity.class);
                sub.putExtra("group_id", attendanceData.getGroupId());
                sub.putExtra("form_id", attendanceData.get_id());
                startActivity(sub);

                break;
        }
    }

    /**
     * 考勤提醒
     */
    private void setAttendanceNotificationMsg() {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin.getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_ATTENDANCE_FORM_ID, attendanceData.get_id()));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_GROUP_ID, attendanceData.getGroupId()));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_IS_NOTIFICATION, getNotice() + ""));
        GetMsgHttpReceiver msg = new GetMsgHttpReceiver(mHandler.obtainMessage(SET_NOTIFICATION));
        msg.mParams = nvps;
        msg.mApi = AsynHttpClient.API_EVENT_ATTENDANCE_NOTIFICATION;
        AsynHttpClient.getInstance(this).execEventHttp(msg);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                Intent back = new Intent();
                back.putExtra("attendance", attendanceData);
                setResult(RESULT_OK, back);
                finish();
                break;
        }

        return true;
    }
}
